package cn.peach.org.controller;

import cn.peach.basic.exception.MyException;
import cn.peach.basic.util.AjaxResult;
import cn.peach.basic.util.PageList;
import cn.peach.org.domain.Shop;
import cn.peach.org.query.ShopQuery;
import cn.peach.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "员工接口", description = "店铺接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据", notes = "")
    public Shop findById(@PathVariable("id") Long id) {
        return  shopService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有",notes = "")
    public List<Shop> findAll() {
        return shopService.findAll();
    }

    @PostMapping
    @ApiOperation(value = "分页查询",notes = "")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryList(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除",notes = "传一个id")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("垃圾系统。。。");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "传数组id")
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            shopService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统崩坏");
        }
    }

    @PutMapping
    @ApiOperation(value = "店铺添加或修改",notes = "有id就是修改反之就是添加")
    public AjaxResult addAndUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() == null){
                shopService.insert(shop);
            }else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("垃圾系统404");
        }
    }
    @PostMapping("/settlement")
    public AjaxResult shopGoin(@RequestBody Shop shop) {
        try {
            shopService.settledIn(shop);
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("垃圾GUI系统");
        }
    }

}
